<template>
  <div id="app">
    <template v-if="route.path.startsWith('/user')">
      <router-view />
    </template>
    <template v-else>
      <BasicLayout />
    </template>
  </div>
</template>

<style>
#app {
}
</style>
<script setup lang="ts">
import BasicLayout from "@/layouts/BasicLayout";
import { onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";

const route = useRoute();
const router = useRouter();

/**
 * 检查 URL 中的 token 参数
 */
const checkUrlToken = () => {
  const urlParams = new URLSearchParams(window.location.search);
  const token = urlParams.get("token");
  console.log(token);
  if (token) {
    // 保存 token 到 localStorage
    localStorage.setItem("token", token);
    // 使用 router.push 进行导航，保持当前路径
    router.push({
      path: route.path,
      replace: true,
    });
  }
};

/**
 * 全局初始化函数，有全局单次调用的代码，都可以写到这里
 */
const doInit = () => {
  console.log("hello 欢迎来到我的项目");
  checkUrlToken();
};

onMounted(() => {
  doInit();
});
</script>
